package com.md24housecall.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.androidquery.AQuery;
import com.astuetz.PagerSlidingTabStrip;
import com.md24housecall.activity.R;
import com.md24housecall.adapter.FragmentAdapter;
import com.md24housecall.manager.CacheManager;
import com.md24housecall.util.GlobalConstant;
import com.md24housecall.util.MyLog;
import com.md24housecall.util.Utils;
import com.md24housecall.util.Utils.OnRequestCategoryListner;

public class MainFragment extends Fragment{

	private FragmentAdapter mAdapter;
	private ViewPager mPager;
//	private TabPageIndicator mIndicator;
	private GlobalConstant constant=GlobalConstant.getInstance();
	private Utils util = Utils.getInstance();
	private AQuery aQuery;
	private CacheManager cacheManager;
	private int totalCategory=0;
	private int totalPartner=0;
	private PagerSlidingTabStrip tabs;
	private String code = "";
	private Handler mHandler=new Handler();
	private Runnable mRunnable;
	private String[]contents;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		final View view=inflater.inflate(R.layout.fragment_main, container, false);
		code = util.getCode(getActivity());
		MyLog.d(GlobalConstant.TAG, "code remember if exsist 123: "+code);
		constant.userType=util.getUserType(getActivity());
		final FragmentActivity act=getActivity();
		mRunnable=new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				if(!code.equals("")){
					contents = getActivity().getResources().getStringArray(R.array.tabs_name_login);
					if(constant.userType==constant.CENTER_USER){
						contents[contents.length-1]=act.getResources().getString(R.string.center_tab_name);
					}
					constant.code=code;
				}else{
					contents = act.getResources().getStringArray(R.array.tabs_name);
				}
				constant.isUpdateIhealthUser=true;
				constant.isUpdatePatient=true;
				mAdapter = new FragmentAdapter(act,contents);
				mPager = (ViewPager)view.findViewById(R.id.pager);
				mPager.setAdapter(mAdapter);
				tabs = (PagerSlidingTabStrip)view.findViewById(R.id.tabs);	
				tabs.setTabPaddingLeftRight(5);
				tabs.setShouldExpand(true);
				mPager.setOffscreenPageLimit(mAdapter.getCount());
				tabs.setViewPager(mPager);
//				constant.mainActivity=this;
//				MyLog.d(GlobalConstant.TAG, "main activity");
				aQuery=new AQuery(act);
				cacheManager=CacheManager.getInstance(act);
				totalCategory=cacheManager.getTotalPartnerCategory();
				totalPartner=cacheManager.getTotalPartners();
				if(totalPartner==0 || constant.isRetryRequestPartner)
					util.getPartnerFromApi(aQuery, cacheManager, false);
				if(totalCategory==0 || constant.isRetryRequestCategory){
					util.getPartnerCategoryFromApi(aQuery, cacheManager, false);
					util.setOnRequestCommplete(new OnRequestCategoryListner() {
						
						@Override
						public void onRequestComplete() {
							// TODO Auto-generated method stub
							MyLog.d(GlobalConstant.TAG, "on retry category complete");
							getActivity().sendBroadcast(new Intent(constant.ACTON_REFRESH));
						}
					});
				}
			}
		};
		
		mHandler.postDelayed(mRunnable, 500);
		return view;
	}
	
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		mAdapter=null;
		mHandler.removeCallbacks(mRunnable);
		super.onDestroy();
	}
}
